﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media.Imaging;

namespace RoboRun
{
    class Scoring
    {
        public static int totalScore = 0;
        public static bool timerOn = false;
        private static int commandCount = GameplayScreen.commandQueue.Count;
        public static string advice = "";

        //private static async void runtimer()
        //{
        //    while(timerOn)
        //    {
        //    await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1000));
        //    count ++;
        //    }
        //}

        private static void evaluateCommandQueue()
        {
            foreach (String command in GameplayScreen.commandQueue)
            {
                if (command.Contains("Straight") || command.Contains("Clockwise"))
                {
                    totalScore += 500;
                }
                else if (command.Contains("For"))
                {
                    totalScore += 1000;
                }
                else if (command.Contains("If"))
                {
                    totalScore += 700;
                }
                else if (command.Contains("While"))
                {
                    totalScore += 1200;
                }
                else if (command.Contains("While") && command.Contains("If"))
                {
                    totalScore += 1500;
                }
                else if (command.Contains("For") && command.Contains("If"))
                {
                    totalScore += 1500;
                }

                else if (command.Contains("[") || command.Contains("]"))
                {
                    commandCount--;
                }
            }
        }

        public static void analyseUserCoder()
        {
            bool forMarker = false;
            bool ifMarker = false;
            bool whileMarker = false;
            foreach (String command in GameplayScreen.commandQueue)
            {
                if (command.Contains("For"))
                {
                    forMarker = true;
                }
                else if (command.Contains("If"))
                {
                    ifMarker = true;
                }
                else if (command.Contains("While"))
                {
                    whileMarker = true;
                }
            }

            if ((!forMarker) && (!ifMarker) && (!whileMarker))
            {
                advice = "It seems you haven't used any loops. Using loops will get you more points as fewer lines of code can be written at a quicker time!";
                GameplayScreen.hintText.Source = new BitmapImage(new Uri("ms-appx:///Assets/GameplayScreen/Hints/loopshint.png", UriKind.Absolute
                      ));
                GameplayScreen.hintText.Width = 1842 * GameplayScreen.width / 1280;
                GameplayScreen.hintText.Height = 31 * GameplayScreen.height / 800;
            }
            else if ((forMarker) && (!whileMarker))
            {
                advice = "Instead of using a For loop, try using a While loop if possible to make your algorithm more intelligent!";
                GameplayScreen.hintText.Source = new BitmapImage(new Uri("ms-appx:///Assets/GameplayScreen/Hints/whileloophint.png", UriKind.Absolute
                      ));
                GameplayScreen.hintText.Width = 1842 * GameplayScreen.width / 1280;
                GameplayScreen.hintText.Height = 31 * GameplayScreen.height / 800;
            }
            else if ((whileMarker) && (!ifMarker))
            {
                advice = "When using a While loop, use an If statement where possible to make your algorithm more intelligent!";
                GameplayScreen.hintText.Source = new BitmapImage(new Uri("ms-appx:///Assets/GameplayScreen/Hints/whileifelsehint.png", UriKind.Absolute
                      ));
                GameplayScreen.hintText.Width = 1842 * GameplayScreen.width / 1280;
                GameplayScreen.hintText.Height = 31 * GameplayScreen.height / 800;
            }
            else if ((!ifMarker))
            {
                advice = "Try using if statements to create a more intelligent algorithm!";
                GameplayScreen.hintText.Source = new BitmapImage(new Uri("ms-appx:///Assets/GameplayScreen/Hints/ifhint.png", UriKind.Absolute
                      ));
                GameplayScreen.hintText.Width = 1842 * GameplayScreen.width / 1280;
                GameplayScreen.hintText.Height = 31 * GameplayScreen.height / 800;
            }
        }

        private static void deductNumberOfLines()
        {
            int pointsToDeduct = commandCount * 50;
            totalScore -= pointsToDeduct;
        }

        private static void deductPointsForTime()
        {
            int pointsToDeduct = (GameplayScreen.getCurrentTimerTicks() / 50) * 10;
            totalScore -= pointsToDeduct;
        }


        public static void printAdvice()
        {
            analyseUserCoder();
            //GameplayScreen.adviceBox.Text = advice;
        }

        public static int getTotalScore()
        {
            evaluateCommandQueue();
            deductNumberOfLines();
            deductPointsForTime();
            if (totalScore < 0)
            {
                return 0;
            }
            else return totalScore;
        }
    }
}
